package com.future.hedongjun.home.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.activity.BaseListActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.view.IosAlertDialog;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.home.adapter.MessageAdapter;
import com.future.hedongjun.home.bean.MessageBean;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class MessageActivity extends BaseListActivity implements BaseQuickAdapter.OnItemChildClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("消息");
        setTitleRight("清空", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new IosAlertDialog(MessageActivity.this)
                        .builder()
                        .setMsg("确定清空所有消息吗?")
                        .setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        })
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                clearMsg();
                            }
                        }).show();
            }
        });
        adapter.setOnItemChildClickListener(this);
    }

    private void clearMsg() {
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setApiCode("")//todo 清空消息
                .addParam("userToken", MyApp.getInstance().getUserToken())
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        if (((DefaultResponse) response).data.contains("true")) {
                            if (adapter != null) {
                                adapter.setNewData(null);
                            }
                        }
                    }
                }).start(new DefaultResponse());
    }

    @Override
    protected BaseQuickAdapter getAdapter() {
        return new MessageAdapter(null);
    }


    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<MessageBean>>(){}.getType();
    }

    @Override
    protected TreeMap<String, String> getRequestParams() {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("apiCode","_mynewslist_001");
        map.put("userToken",MyApp.getInstance().getUserToken());
        return map;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        toast("消息详情");
    }
}
